<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>角色管理</title>
		<link href="../../component/pear/css/pear.css" rel="stylesheet" />
	</head>
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">账户名</label>
							<div class="layui-input-inline">
								<input type="text" name="username" placeholder="查询不区分大小写" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">账户状态</label>
							<div class="layui-input-block">
								<select name="enabled" type="text">
									<option value=""></option>
									<option value="启用">启用</option>
									<option value="禁用">禁用</option>
								</select>
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">账户权限</label>
							<div class="layui-input-block">
								<select name="userface" type="text">
									<option value=""></option>
									<option value="超级管理员">超级管理员</option>
									<option value="普通管理员">普通管理员</option>
									<option value="普通用户">普通用户</option>
									<option value="游客">游客</option>
								</select>
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="role-query">
								<i class="layui-icon layui-icon-search"></i>
								查询
							</button>
							<button type="reset" class="pear-btn pear-btn-md" id="resetAll">
								<i class="layui-icon layui-icon-refresh"></i>
								重置
							</button>
						</div>
					</div>
				</form>
			</div>
		</div>
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="role-table" lay-filter="role-table"></table>
			</div>
		</div>
		
		<script type="text/html" id="role-toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
				<i class="layui-icon layui-icon-add-1"></i>
				新增
			</button>
			<button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
				<i class="layui-icon layui-icon-delete"></i>
				批量删除
			</button>
			<button class="pear-btn pear-btn-warming pear-btn-sm" lay-event="power">
				<i class="layui-icon layui-icon-vercode"></i>
				授权
			</button>
		</script>
		
		<script type="text/html" id="role-bar">
			<button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
		</script>

		<div class="layui-card">
			<div class="layui-card-header">
				大 佬 语 录
			</div>
			<div class="layui-card-body">
				<!--这儿是留言板内容，下面代码用jquery填充-->
				<blockquote class="layui-elem-quote">成长就是你最终会发现，世界上啥人都有</blockquote>
				<!--这儿是留言板内容，下面代码用jquery填充-->
			</div>
		</div>

		<script src="../../component/layui/layui.js"></script>
		<script src="../../component/pear/pear.js"></script>
		<script>
		    layui.use(['table','form','jquery'],function () {
		        let table = layui.table;
		        let form = layui.form;
		        let $ = layui.jquery;
		
		        let MODULE_PATH = "operate/";
		        let cols = [
		            [
		                {type:'checkbox'},
		                {title: '账户权限', field: 'userface', align:'center', width:100},
		                {title: '账户名', field: 'username', align:'center'},
		                {title: '座右铭', field: 'remark', align:'center'},
		                {title: '账户状态', field: 'enabled', align:'center'},
		                {title: '删除', toolbar: '#role-bar', align:'center', width:195}
		            ]
		        ]

				// 监听按钮事件
				$("#resetAll").click(function () {
					table.render({
						elem: '#role-table',
						url: '/Admin/getAllUser',
						page: true ,
						cols: cols ,
						skin: 'line',
						toolbar: '#role-toolbar',
						defaultToolbar: [{
							title: '刷新',
							layEvent: 'refresh',
							icon: 'layui-icon-refresh',
						}, 'filter', 'print', 'exports']
					});
				});

		        table.render({
		            elem: '#role-table',
		            url: '/Admin/getAllUser',
		            page: true ,
		            cols: cols ,
		            skin: 'line',
		            toolbar: '#role-toolbar',
		            defaultToolbar: [{
						title: '刷新',
		                layEvent: 'refresh',
		                icon: 'layui-icon-refresh',
		            }, 'filter', 'print', 'exports']
		        });

		        // 操作面板
		        table.on('tool(role-table)', function(obj){
		            if(obj.event === 'remove'){
		                window.remove(obj);
		            } else if(obj.event === 'edit'){
		                window.edit(obj);
		            } else if(obj.event === 'power'){
		                window.power(obj);
		            }
		        });
		
		        table.on('toolbar(role-table)', function(obj){
		            if(obj.event === 'add'){
		                window.add();
		            } else if(obj.event === 'refresh'){
		                window.refresh();
		            } else if(obj.event === 'batchRemove'){
		                window.batchRemove(obj);
		            } else if(obj.event === 'power'){
						window.power(obj);
					}
		        });

				// 这儿是设置查询条件，进行查询
				form.on('submit(role-query)', function(data) {

					table.render({
						elem: '#role-table',
						url: '/Admin/super/findUser',
						where: data.field,
						contentType: "application/json",
						method: 'post',
						page: true,
						cols: cols,
						skin: 'line',
						toolbar: '#role-toolbar',
						defaultToolbar: [{
							title: '刷新',
							layEvent: 'refresh',
							icon: 'layui-icon-refresh',
						}, 'filter', 'print', 'exports']
					});
					return false;
				});
		

		        window.add = function(){
		            layer.open({
		                type: 2,
		                title: '新增',
		                shade: 0.1,
		                area: ['500px', '500px'],
		                content: MODULE_PATH + 'addUser.html'
		            });
		        }
		
		        window.power = function(obj){
		            layer.open({
		                type: 2,
		                title: '授权',
		                shade: 0.1,
		                area: ['400px', '400px'],
		                content: MODULE_PATH + 'powerEdit.html'
		            });
		        }
		
		        window.remove = function(obj){
		            layer.confirm('确定要删除该账户?', {icon: 3, title:'提示'}, function(index){
		                layer.close(index);
		                let loading = layer.load();
						$.ajax({
							url: "/Admin/super/deleteUser/" + obj.data['username'],
							dataType: 'json',
							type: 'get',
							success: function(result) {
								layer.close(loading);
								if (result.code === 0) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										obj.del();
									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
		            });
		        }
		
		        window.batchRemove = function(obj){
		            let data = table.checkStatus(obj.config.id).data;
		            if(data.length === 0){
		                layer.msg("未选中数据",{icon:3,time:1000});
		                return false;
		            }
		            var names = [];
		            for(let i = 0;i<data.length;i++){
		            	names[i] = data[i].username;
		            }
					layer.confirm('确定要删除选中账户？', {icon: 3, title:'提示'}, function(index){
		                layer.close(index);
		                let loading = layer.load();
		                $.ajax({
		                    url: "/Admin/super/batchRemoveUser",
							data: JSON.stringify(names),
							contentType: "application/json",
		                    dataType:'json',
		                    type:'post',
							success: function(result) {
								layer.close(loading);
								if (result.code === 0) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										table.reload('role-table');
									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
		                })
		            });
		        }
		
		        window.refresh = function(){
					table.render({
						elem: '#role-table',
						url: '/Admin/getAllUser',
						page: true ,
						cols: cols ,
						skin: 'line',
						toolbar: '#role-toolbar',
						defaultToolbar: [{
							title: '刷新',
							layEvent: 'refresh',
							icon: 'layui-icon-refresh',
						}, 'filter', 'print', 'exports']
					});
		        }
		    })
		</script>
	</body>
</html>
